package test;

import java.io.*;
import java.util.ArrayList;
import java.util.List;

public class Transform {
    private static List<String> bytesToHexString2(byte[] src) {
        if (src == null || src.length <= 0) {
            return null;
        }
        List<String> list = new ArrayList<String>();
        for (byte aSrc : src) {
            int v = aSrc & 0xFF;
            String s = Integer.toHexString(v);
            StringBuilder stringBuilder = new StringBuilder("");
            if (s.length() < 2) {
                stringBuilder.append(0);
            }
            stringBuilder.append(s);
            list.add(stringBuilder.toString());
        }
        return list;
    }

    public static String bytesToHexString1(byte[] src) {
        StringBuilder stringBuilder = new StringBuilder("");
        if (src == null || src.length <= 0) {
            return null;
        }
        for (int i = 0; i < src.length; i++) {
            int v = src[i] & 0xFF;
            String hv = Integer.toHexString(v);
            if (hv.length() < 2) {
                stringBuilder.append(0);
            }
            stringBuilder.append(hv);
        }
        return stringBuilder.toString();
    }

    private void transFile() {
        String inputFlie = "E:\\self\\transformecg\\src\\2018-06-12-22-33-05-RawPackageRecording";
        File file = new File(inputFlie + ".txt");
        File outFile = new File(inputFlie + "_trans.txt");
        try {
            FileWriter fw = new FileWriter(outFile);
            byte[] b = new byte[2048];
            FileInputStream fis = new FileInputStream(file);
            int len = 0;
            List<String> hexList = new ArrayList<>();
            while ((len = fis.read(b)) != -1) {
                byte[] c = new byte[len];
                System.arraycopy(b, 0, c, 0, len);
                List<String> list = bytesToHexString2(c);
                hexList.addAll(list);
            }
            for (int i = 0; i < hexList.size() / 2; i += 1) {
                 Integer d1 = Integer.parseInt(hexList.get(i * 2), 16) << 8;
                Integer d2 = Integer.parseInt(hexList.get(i * 2 + 1), 16);
                int raw = d1 + d2;
                if (raw >= 32768) raw = raw - 65536;
//                raw = 0 - raw;
                fw.write(raw + "");
                if (i >= 2047) {
                     break;
                }
                fw.write("\r\n");
            }
            fw.flush();
            fw.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
        Transform t = new Transform();
        t.transFile();
//        Integer d1 = Integer.parseInt("0d", 16) << 8;
//        Integer d2 = Integer.parseInt("9f", 16);
//        System.out.println(d1+d2);
    }
}
